﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    /// <summary>
    /// Interaction logic for EnvironmentalProfilesPanel.xaml
    /// </summary>
    public partial class EnvironmentalProfilesPanel : UserControl
    {
        private EnvironmentalProfilesViewModel mViewModel = null;
        public EnvironmentalProfilesPanel()
        {
            InitializeComponent();
            if (null == mViewModel)
            {
                mViewModel = EnvironmentalProfilesViewModel.GetInstance();
                this.DataContext = mViewModel;
            }
        }

        private void OnTextBoxPreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var obj = sender as UIElement;
                BindingExpression textBinding = BindingOperations.GetBindingExpression(obj, TextBox.TextProperty);
                if (textBinding != null) textBinding.UpdateSource();
            }
        }

        private void OnCanExecuteNewCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
        private void OnExecuteNewCommand(object sender, ExecutedRoutedEventArgs e)
        {
            EnvironmentalProfileViewModel _newly=new EnvironmentalProfileViewModel();
            mViewModel.Profiles.Add(_newly);
            mViewModel.Profile = _newly;
        }

        private void OnCanExecuteRemoveCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = null == mViewModel ? false : null != mViewModel.Profile;
        }
        private void OnExecuteRemoveCommand(object sender, ExecutedRoutedEventArgs e)
        {
            mViewModel.Profiles.Remove(mViewModel.Profile);
            mViewModel.Profile = null;
        }
    }
}
